package com.Glow.reggie.controller;

import com.Glow.reggie.common.BaseContext;
import com.Glow.reggie.common.R;
import com.Glow.reggie.entity.ShoppingCart;
import com.Glow.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-08-05 19:52:07
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("list")
    public R<List<ShoppingCart>> list()
    {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart)
    {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart)
    {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        shoppingCartService.remove(queryWrapper);
        return R.success("溢出成功！");
    }
    @DeleteMapping("clean")
    public R<String> clean()
    {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功！");
    }
}
